#include <QtGui>
#include <QtCore/QTextCodec>
#include <QTranslator>
#include "gv_setupchangepwddiaglog.h"
#include "gv_setuphomedialog.h"
#include "gv_home.h"
#include "gv_setting.h"

extern GV_Setting *gvsetting;
extern QDialog  *dialog;
GV_SetupChangePWDDialog::GV_SetupChangePWDDialog(QWidget *parent)
       : QDialog(parent)
{
    setupUi(this);

    QTranslator translator;
    translator.load(gvsetting->GV_GetLanguage(),".");
    qApp->installTranslator(&translator);
    reloadtext();

    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));
PasswordSetupChangePWDLCD->display("----------");
    currentNum = 0;
    totalNum = 0;
    newpassword=conformpassword = 0;
    newpasswodButtonEnable = true;

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(GV_chTime()));
    connect(AcceptButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(CancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(BackButton,SIGNAL(clicked()),this,SLOT(cancel()));

    connect(N0SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key0()));
    connect(N1SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key1()));
    connect(N2SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key2()));
    connect(N3SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key3()));
    connect(N4SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key4()));
    connect(N5SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key5()));
    connect(N6SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key6()));
    connect(N7SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key7()));
    connect(N8SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key8()));
    connect(N9SetupChangePWDButton,SIGNAL(clicked()),this,SLOT(key9()));
    connect(ClearSetupChangePWDButton,SIGNAL(clicked()),this,SLOT(keyclear()));
    connect(BackspaceSetupChangePWDButton,SIGNAL(clicked()),this,SLOT(keybackspace()));

    connect(EnterNewPasswordButton,SIGNAL(clicked()),this,SLOT(enternewpassword()));
    connect(ConformNewPasswordButton,SIGNAL(clicked()),this,SLOT(conformnewpassword()));

    timer->start(1000);
}

void GV_SetupChangePWDDialog::reloadtext()
{
    NameHintsLabel->setText(tr("User's Setup"));

    EnterNewPasswordButton->setText(tr("Enter New Password"));
    ConformNewPasswordButton->setText(tr("Conform New Password"));
    ClearSetupChangePWDButton->setText(tr("Clear"));
    BackspaceSetupChangePWDButton->setText(tr("Backspace"));

    AcceptButton->setText(tr("Accept"));
    CancelButton->setText(tr("Cancel"));
    BackButton->setText(tr("Back"));
}
void GV_SetupChangePWDDialog::GV_chTime()
{
    TimeHintsLabel->setText((QDateTime::currentDateTime().toString("hh:mm:ss")));
    DateHintsLabel->setText((QDateTime::currentDateTime().toString("yyyy-MM-dd")));
}

void GV_SetupChangePWDDialog::cancel()
{
    QDialog *dialog1 = new GV_SetupHomeDialog;
    dialog1->show();
    delete dialog;
    dialog = dialog1;
}

void GV_SetupChangePWDDialog::accept()
{
    //QString str;
    //str = "new:" + QString::number(newpassword) + "conform:" + QString::number(conformpassword);
    //QMessageBox::about(this, "value",str);

    if(newpassword==conformpassword){
        QDialog *dialog1;
        dialog1 = new GV_SetupHomeDialog;
        dialog1->show();
        delete dialog;
        dialog = dialog1;
    }else{

    }

}

void GV_SetupChangePWDDialog::showNumber()
{
    if(totalNum < 1000000000){
        totalNum = totalNum*10 + currentNum;
        PasswordSetupChangePWDLCD->display(totalNum);
        if(newpasswodButtonEnable){
            newpassword = totalNum;
        }else{
            conformpassword = totalNum;
        }
    }
}
void GV_SetupChangePWDDialog::key0()
{
    currentNum = 0;
    showNumber();
}
void GV_SetupChangePWDDialog::key1()
{
    currentNum = 1;
    showNumber();
}

void GV_SetupChangePWDDialog::key2()
{
    currentNum = 2;
    showNumber();
}
void GV_SetupChangePWDDialog::key3()
{
    currentNum = 3;
    showNumber();
}
void GV_SetupChangePWDDialog::key4()
{
    currentNum = 4;
    showNumber();
}
void GV_SetupChangePWDDialog::key5()
{
    currentNum = 5 ;
    showNumber();
}
void GV_SetupChangePWDDialog::key6()
{
    currentNum = 6;
    showNumber();
}
void GV_SetupChangePWDDialog::key7()
{
    currentNum = 7;
    showNumber();
}
void GV_SetupChangePWDDialog::key8()
{
    currentNum = 8;
    showNumber();
}
void GV_SetupChangePWDDialog::key9()
{
    currentNum = 9;
    showNumber();
}
void GV_SetupChangePWDDialog::keyclear()
{
    currentNum = 0;
    totalNum = 0;
    showNumber();
}
void GV_SetupChangePWDDialog::keybackspace()
{
    totalNum = totalNum / 10;
    currentNum = totalNum%10;
    totalNum = totalNum / 10;

    showNumber();
}

void GV_SetupChangePWDDialog::enternewpassword()
{
    currentNum = 0;
    totalNum = 0;
    newpasswodButtonEnable = true;
    showNumber();
}
void GV_SetupChangePWDDialog::conformnewpassword()
{
    currentNum = 0;
    totalNum = 0;
    newpasswodButtonEnable = false;
    showNumber();
}
